﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GrewalCRM.Entities;

namespace GrewalCRM.DAL
{
  public class ACBrands_DAL : BaseDALGrewalCRM, IBaseDALGrewalCRM
  {
    #region Protected Methods
    public override int AddNew(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      ACBrands acBrand = Entity as ACBrands;
      Cmd.CommandText = "usp_InsertACBrands";
      SetParametersForDBSave(acBrand);
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public override int Update(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      ACBrands acBrand = Entity as ACBrands;
      Cmd.CommandText = "usp_UpdateACBrands";
      SetParametersForDBSave(acBrand);
      Cmd.Parameters.AddWithValue("@ACBrandID", acBrand.ID);
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public override int Delete(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      ACBrands acBrand = Entity as ACBrands;
      Cmd.CommandText = "usp_DeleteACBrands";
      SetParametersForDBSave(acBrand);
      Cmd.Parameters.AddWithValue("@ACBrandID", acBrand.ID);
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public int Save(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }

    public override List<BaseEntitiesGrewalCRM> GetAll()
    {

      Cmd.CommandText = "usp_GetAllACBrand";     
      Cmd.Connection.Open();
      SqlDataReader rdr = Cmd.ExecuteReader();
      List<BaseEntitiesGrewalCRM> allBrands = new List<BaseEntitiesGrewalCRM>();

      if (rdr != null && rdr.HasRows)
      {
        while (rdr.Read())
        {
          UserCRM lasrModifiedUser = new UserCRM(rdr[4] != null && rdr[4].GetType() != typeof(DBNull) ? rdr.GetInt32(4) : 0);
          lasrModifiedUser.UserName = rdr[5] != null && rdr[5].GetType() != typeof(DBNull) ? rdr.GetString(5) : string.Empty;

          ACBrands acBrand = new ACBrands(rdr[0] != null && rdr[0].GetType() != typeof(DBNull) ? rdr.GetInt32(0) : 0,
                                                  rdr[1] != null && rdr[1].GetType() != typeof(DBNull) ? rdr.GetString(1) : string.Empty,
                                                  rdr[2] != null && rdr[2].GetType() != typeof(DBNull) ? rdr.GetString(2) : string.Empty,                                                  
                                                  rdr[3] != null && rdr[3].GetType() != typeof(DBNull) ? rdr.GetBoolean(3) : false,
                                                  lasrModifiedUser,
                                                  rdr[6] != null && rdr[6].GetType() != typeof(DBNull) ? rdr.GetDateTime(6) : DateTime.MinValue
                                                 );


          allBrands.Add(acBrand);
        }
      }
      Cmd.Connection.Close();

      return allBrands;      
    }

    public override BaseEntitiesGrewalCRM GetByID(int ID)
    {
        Cmd.CommandText = "usp_GetACBrandByID";
        Cmd.Parameters.AddWithValue("@ACBrandID", ID);
        Cmd.Connection.Open();
        Rdr = Cmd.ExecuteReader();
        List<BaseEntitiesGrewalCRM> allACBrands = new List<BaseEntitiesGrewalCRM>();

        if (Rdr != null && Rdr.HasRows)
        {
            while (Rdr.Read())
            {


                UserCRM lasrModifiedUser = new UserCRM(Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetInt32(4) : 0);

                ACBrands acBrand = new ACBrands(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                                        Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull) ? Rdr.GetString(1) : string.Empty,
                                                        Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetString(2) : string.Empty,
                                                        Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetBoolean(3) : false,
                                                        lasrModifiedUser,                                                        
                                                        Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetDateTime(6) : DateTime.MinValue
                                                       );
                allACBrands.Add(acBrand);
            }
        }
        Cmd.Connection.Close();

        if (allACBrands.Count > 0)
            return allACBrands[0];
        else
            return null;
    }

    public override void FillEntity(BaseEntitiesGrewalCRM Entity)
    {
        Entity = GetByID(Entity.ID);
    } 
    #endregion

    #region Private Method
    private void SetParametersForDBSave(ACBrands acBrand)
    {
      Cmd.Parameters.Clear();
      Cmd.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

      if (acBrand.ACBrandName != null && acBrand.ACBrandName.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@ACBrandName", acBrand.ACBrandName);
      
        Cmd.Parameters.AddWithValue("@IsActive", acBrand.IsActive);

      if (acBrand.Description != null && acBrand.Description.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@Description", acBrand.Description);

      if (null != acBrand.LastModifiedUser && acBrand.LastModifiedUser.ID != 0)
        Cmd.Parameters.AddWithValue("@LastModifiedUser", acBrand.LastModifiedUser.ID);
    }
    #endregion
  }
}
